package com.ruoyi.report.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.exception.GlobalException;
import com.ruoyi.report.model.DataEntity;
import com.ruoyi.report.model.RepoTable;
import com.ruoyi.report.model.dto.repo.DataSourceDTO;
import com.ruoyi.report.model.dto.repo.FilterCriteria;
import com.ruoyi.report.model.vo.BillResultVO;
import com.ruoyi.report.service.table.interfaces.BillMainService;
import com.ruoyi.report.service.table.interfaces.CollectResultMainService;
import com.ruoyi.report.service.table.interfaces.RepoTableService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class repoServiceImpl implements RepoService {


    @Resource
    private CollectResultMainService collectResultMainService;

    @Resource
    BillMainService billMainService;

    @Resource
    private RepoTableService repoTableService;


    @Override
    public Map<String, Map<String, Double>> getData(DataSourceDTO dataSourceDTO){
        //根据接口名判断调取哪个接口
        String sourceInterfaceName = dataSourceDTO.getSourceInterfaceName();
        if ("collectResult".equals(sourceInterfaceName)){
            return  collectResultMainService.list(dataSourceDTO,dataSourceDTO.getFilterCriteria());
        }else if ("billMain".equals(sourceInterfaceName)){
            return billMainService.list(dataSourceDTO,dataSourceDTO.getFilterCriteria());
        }
        return null;
    }

    @Override
    public DataEntity convertData(String repoCode) {
        //根据repoCode获取数据
        RepoTable repoTable = repoTableService.getById(repoCode);
        if (repoTable!=null){
            String repoParam = repoTable.getRepoParam();
            //repoParam转换为数据实体类DataSourceDTO
            DataSourceDTO dataSourceDTO = JSON.parseObject(repoParam, DataSourceDTO.class);
            if (dataSourceDTO==null){
                return null;
            }
            String repoYAxis = repoTable.getRepoYAxis();
            Map<String, String> yAxisMap = JSON.parseObject(repoYAxis, new TypeReference<Map<String, String>>(){});
            Map<String, Map<String, Double>> mapMap = getData(dataSourceDTO);
            return convertData(repoTable.getRepoType(), mapMap,yAxisMap);
        }
        return null;
    }

    //查询所有配置信息
    @Override
    public List<RepoTable> list(RepoTable repoTable) {
        return repoTableService.list(new LambdaQueryWrapper<>(repoTable));
    }

    //根据id获取配置信息
    @Override
    public RepoTable getById(String repoCode) {
        return repoTableService.getById(repoCode);
    }


    //保存和修改配置信息
    @Override
    public void saveOrUpdateRepo(RepoTable repoTable) {
        repoTableService.saveOrUpdate(repoTable);
    }

    //删除配置信息
    @Override
    public void deleteRepo(String repoCode) {
        repoTableService.removeById(repoCode);
    }



    //根据类型判断用哪个方法转换
    public DataEntity convertData(RepoTable.RepoTypeEnum type, Map<String, Map<String, Double>> originalData, Map<String, String> yAxisMap) {
        switch (type) {
            case ROSE_CHART:
            case RING_CHART:
            case VERTICAL_BAR_CHART:
            case HORIZONTAL_BAR_CHART:
                return convertDataTwo(originalData,yAxisMap);
            case VERTICAL_DUAL_AXIS_BAR_CHART:
            case MULTILINE_CHART:
                return convertDataOne(originalData,yAxisMap);
            default:
                throw new GlobalException("不支持的图表类型");
        }
    }


    //纵向双轴柱状图  多折线图
    public static DataEntity convertDataOne(Map<String, Map<String, Double>> originalData,Map<String, String> yAxisMap) {
        List<DataEntity> nestedAcceptanceData = new ArrayList<>();
        for (Map.Entry<String, Map<String, Double>> entry : originalData.entrySet()) {
            DataEntity dataEntity = new DataEntity();
            dataEntity.setCategory(entry.getKey());
            List<Map<String, Object>> dataList = new ArrayList<>();
            for (Map.Entry<String, Double> innerEntry : entry.getValue().entrySet()) {
                Map<String, Object> dataItem = new HashMap<>();
                dataItem.put("name", innerEntry.getKey());
                String yName = yAxisMap.get(innerEntry.getKey());
                if (yName!=null){
                    dataItem.put("name", yName);
                }
                dataItem.put("value", innerEntry.getValue());
                dataList.add(dataItem);
            }
            dataEntity.setDataList(dataList);
            nestedAcceptanceData.add(dataEntity);
        }
        return DataEntity.builder().data(nestedAcceptanceData).build();
    }

    //横向柱状图 纵向单轴柱状图转换
    public static DataEntity convertDataTwo(Map<String, Map<String, Double>> originalData, Map<String, String> yAxisMap) {
        List<Map<String, Object>> resultList = new ArrayList<>();
        String unit = "";
        for (Map.Entry<String, Map<String, Double>> entry : originalData.entrySet()) {
            Map<String, Object> companyData = new HashMap<>();
            companyData.put("name", entry.getKey());
            for (Map.Entry<String, Double> innerEntry : entry.getValue().entrySet()) {
                companyData.put("value", innerEntry.getValue());
                unit=innerEntry.getKey();
                String yName = yAxisMap.get(innerEntry.getKey());
                if (yName!=null){
                    unit=yName;
                }
            }
            resultList.add(companyData);
        }
        return DataEntity.builder().unit(unit).dataList(resultList).build();
    }


}
